package com.wb.structual.proxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.SimpleFormatter;

/**
 * @author 魏彪
 */
public class LogHandler implements InvocationHandler {

    // 提供一个真实主题
    private Object object;

    public LogHandler(Object object) {
        this.object = object;
    }

    // 调用在真实主题中的方法
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        Object result = method.invoke(object, args);
        after();
        return result;
    }

    public void before() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("当前调用时间" + sdf.format(new Date()));
    }

    public void after() {
        System.out.println("方法调用结束");
    }
}
